package com.batchaps.global;

import javax.microedition.lcdui.*;

import java.util.*;

 public   class Utils
    {
        static Random m_rnd = new Random(System.currentTimeMillis());

        public static int m_iClipRectX = 0;
        public static int m_iClipRectY = 0;
        public static int m_iClipRectW = 240;
        public static int m_iClipRectH = 400;

        public static void SaveClip(Graphics g)
        {
            m_iClipRectX = g.getClipX();
            m_iClipRectY = g.getClipY();
            m_iClipRectW = g.getClipWidth();
            m_iClipRectH = g.getClipHeight();
        }

        public static void LoadClip(Graphics g)
        {
            g.setClip(m_iClipRectX, m_iClipRectY, m_iClipRectW, m_iClipRectH);
        }

        public static void ResetClip(Graphics g)
        {
            g.setClip(0, 0, GameEngine.k_Screen_Width, GameEngine.k_Screen_Height);
        }

        public static void ClearScreen(Graphics g)
        {
            g.setColor(0);
            g.fillRect(0, 0, GameEngine.k_Screen_Width, GameEngine.k_Screen_Height);
        }

        public static String GetResourcePath(String file)
        {
            return "/" + file;
        }

        public static Random GetRandom()
        {
            return m_rnd;
        }
        
        public static Image resizeImage(Image src, int screenWidth, int screenHeight)
        {
            int srcWidth = src.getWidth();
            int srcHeight = src.getHeight();
            Image tmp = Image.createImage(screenWidth, srcHeight);
            Graphics g = tmp.getGraphics();
            int ratio = (srcWidth << 16) / screenWidth;
            int pos = ratio / 2;

            // Horizontal Resize
            for (int x = 0; x < screenWidth; x++)
            {
                g.setClip(x, 0, 1, srcHeight);
                g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
                pos += ratio;
            }

            Image resizedImage = Image.createImage(screenWidth, screenHeight);
            g = resizedImage.getGraphics();
            ratio = (srcHeight << 16) / screenHeight;
            pos = ratio / 2;

            //Vertical resize
            for (int y = 0; y < screenHeight; y++) {
                g.setClip(0, y, screenWidth, 1);
                g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
                pos += ratio;
            }

            return resizedImage;
        }

    }
